<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Login extends CI_Controller{
    public function __construct() {
        parent::__construct();
        $this->load->model('Login_model');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('session');
    }
    public function index()
    {
        $logined=$this->session->userdata('email');
        if($logined != '')
        {
            redirect('staff/index');
        }
        else
            $this->load->view('login');
    }
    public function home()
    {
       $logined=$this->session->userdata('logined');
        if($logined=='true')
        {
            redirect('staff/index');
        }
        else
            $this->load->view('login'); 
    }
    public function checkLogin()
    {
        $email=trim($this->security->sanitize_filename($this->input->post('txtEmail')));
        $password=trim($this->security->sanitize_filename($this->input->post('txtPass')));
        $query=$this->Login_model->checkLogin($email,$password);
        if($query==0)
        {
            $data['error_login']="Email or Password not correct";
            $data['email']=$email;
            $this->load->view('login',$data);
        }
        else
        {
            foreach ($query as $row)
            {
                $dataLogin=array(   'email'=>$row->email,
                                    'logined'=>'true',
                                    'roles'=>$row->roles);
            }
            $this->session->set_userdata($dataLogin);
            $this->index();
        }   
        //}
    }
    public function loadFormChangePassword()
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        $this->load->view('_header',$data);
        $this->load->view('changePassword');
        $this->load->view('_footer');
    }
    public function changePassword()
    {
        $email=$this->session->userdata('email');
        $data['email']=$email;
        
        $oldpass=trim($this->security->sanitize_filename($this->input->post('txtOldPass')));
        $newpass=trim($this->security->sanitize_filename($this->input->post('txtNewPass')));
        $renewpass=trim($this->security->sanitize_filename($this->input->post('txtReNewPass')));
        $data['oldpass']=$oldpass;
        $data['newpass']=$newpass;
        $data['renewpass']=$renewpass;
        $query=$this->Login_model->checkOldPassword($email,$oldpass);
        if($query->num_rows()==0)
        {
            $data['error_oldpass']="Old Password not correct";
            $this->load->view('_header',$data);
            $this->load->view('changePassword');
            $this->load->view('_footer');
        }
        else
        {
            $this->Login_model->changePassword($email,$newpass);
            redirect('staff/index');
        }
    }
    public function logout()
    {
        $this->session->sess_destroy();
        $this->load->view('login');
    }
}
?>
